package com.sskj.cndragon.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.cndragon.bean.AddressBean;
import com.sskj.cndragon.bean.AssetBean;
import com.sskj.cndragon.bean.AssetListBean;
import com.sskj.cndragon.bean.RecordListBean;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.ui.activity.AssetActivity;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.List;

import io.reactivex.Flowable;


public class AssetActivityPresenter extends BasePresenter<AssetActivity> {
    public void getAsset(int userId) {
        httpService.getAsset(userId)
                .tag(this)
                .execute(new JsonCallBack<HttpData<AssetBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<AssetBean>> response) {
                        HttpData<AssetBean> body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.updataAsset(body.getData());
                        }
                    }
                });
    }

    public Flowable<List<AssetListBean.AssetBean>> getAssetRecord(int type, int page) {
        return httpService.getAssetRecord(SPUtil.get(SPConfig.ID, ""), type, page)
                .tag(this)
                .converter(new JsonConverter<HttpData<AssetListBean>>() {
                })
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData().getList());
    }

    public void getAddresses(String userID) {
        httpService.getAddresses(userID)
                .tag(this)
                .execute(new JsonCallBack<HttpData<List<AddressBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<AddressBean>>> response) {
                        HttpData<List<AddressBean>> body = response.body();
                        if (body.getStatus() == HttpConfig.OK) {
                            mView.getAddressOK(body.getData());
                        } else {
                            ToastUtil.showShort(body.getMsg());
                        }

                    }
                });
    }
}
